<template>
  <div class="top-head">
    <div class="banxin flex">
      <div class="welcom">欢迎来到叩丁狼积分商城</div>
      <div class="right flex">
        <ul class="flex">
          <li>
            <img :src="userInfo.headImg" alt="" />
            <span>用户名：</span>
            <span>{{ userInfo.nickName }}</span>
          </li>
          <li>
            <span>我的鸡腿：</span>
            <span>{{ userInfo.coin }}</span>
          </li>
          <li>
            <span>获取鸡腿</span>
            <span>---</span>
          </li>
          <li>
            <span>叩丁狼官网</span>
          </li>
        </ul>
        <div class="btn" v-show="!isLogin" @click="$store.commit('mod', true)">
          登录
        </div>
        <div class="btn" v-show="isLogin">
          <img src="../../assets/images/Shopping.png" alt="" />
          购物车
          <span>{{ cartTotal }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { getUserInfoApi, WXLoginApi } from "../../http/api";
import headImg from "../../assets/images/userImg.png";
import { toast } from "../../utils/toast";
export default {
  data() {
    return {
      userInfo: {
        headImg,
        nickName: "--",
        coin: "--",
      },
      cartTotal: 0,
      isLogin: false,
    };
  },
  created() {
    // const code = this.$route.query.code;
    // if (code) {
    //   WXLoginApi(code).then((res) => {
    //     if (res.code === 0) {
    //       localStorage.setItem("x-auth-token", res["x-auth-token"]);
    //       toast("微信登录成功", "success");
    //       this.getUserInfo();
    //     }
    //   });
    // }
    this.getUserInfo();
    this.wxLogin();
  },
  methods: {
    wxLogin() {
      const code = this.$route.query.code;
      console.log(code);
      // 证明这是扫码登陆后跳转的
      if (code) {
        // 微信扫码后，需要向服务器发请求来得到token
        WXLoginApi(code).then((res) => {
          // console.log(res);
          if (res.code === 0) {
            localStorage.setItem("x-auth-token", res["x-auth-token"]);
            toast("微信登录成功", "success");
            this.getUserInfo();

            // 把code从路由里面清除掉
            // this.$router.push(this.$route.path);
            if (this.$route.path === "/gooddetail") {
              this.$router.push(
                this.$route.path + "?id=" + localStorage.getItem("detailId")
              );
            } else {
              this.$route.push(this.$route.path);
            }
          }
        });
      }
    },
    async getUserInfo() {
      const token = localStorage.getItem("x-auth-token");
      if (!token) return;
      this.isLogin = true;
      const res = await getUserInfoApi();
      console.log(res.data);
      if (res.code === 0) {
        this.userInfo = res.data.userInfo;
        this.cartTotal = res.data.cartTotal;
        // 要把用户信息存到vuex
        this.$store.commit("updateUserInfo", this.userInfo);
      }
    },
  },
  watch: {
    "$route.query": {
      handler() {
        this.wxLogin();
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
.top-head {
  color: #fff;
  font-size: 14px;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  .banxin {
    justify-content: space-between;
  }
  .right {
    ul {
      li {
        cursor: pointer;
        margin-right: 20px;
        img {
          width: 26px;
          height: 26px;
          vertical-align: middle;
          margin-right: 15px;
          border-radius: 50%;
        }
      }
    }
    .btn {
      cursor: pointer;
      width: 124px;
      text-align: center;
      background: #0a328e;
      display: flex;
      justify-content: space-around;
      align-items: center;
      img {
        width: 20px;
        height: 20px;
      }
      .total {
        width: 22px;
        height: 22px;
        background: #fd604d;
        border-radius: 11px;
        line-height: 22px;
        text-align: center;
      }
    }
  }
}
</style>